package com.example.gcmtest1;

import java.io.File;

import com.example.gcmtest1.youtube.YoutubeDownloader;

import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.support.v4.app.NavUtils;

public class YoutubeActivity extends Activity implements View.OnClickListener {

	public static String VIDEO_FOLDER = "/mnt/sdcard/com.example.gcmtest1/";
	private String logName = "YoutubeActivity";
	
	private View mView;
	private EditText txtYoutubeUrl;
	private Button cmdYoutubeOK, cmdInsertDB;
	private YoutubeDownloader mY;
	
	private ProgressDialog pgDialog;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_youtube);
        
        this.mView = (View) this.getWindow().getDecorView();
        
        this.txtYoutubeUrl = (EditText) this.mView.findViewById(R.id.txtYoutubeUrl);
        this.cmdYoutubeOK = (Button) this.mView.findViewById(R.id.cmdYoutubeOK);
        this.cmdYoutubeOK.setOnClickListener(this);
        
        this.cmdInsertDB = (Button) this.mView.findViewById(R.id.cmdInsertDB);
        this.cmdInsertDB.setOnClickListener(this);
        
        this.pgDialog = new ProgressDialog(this);
        this.pgDialog.setMessage("Loading..");
        this.pgDialog.setTitle("");
        
        File f = new File(VIDEO_FOLDER);
        if (f.exists() == false) {
        	Log.d(logName, "Cannot find VIDEO_FOLDER " + VIDEO_FOLDER + ", going to create one..");
        	boolean b = f.mkdir();
        	if (b == true) {
        		Log.d(logName, "created");
        	} else {
        		Log.e(logName, "failed to create VIDEO_FOLDER " + VIDEO_FOLDER);
        	}
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_youtube, menu);
        return true;
    }

	@Override
	public void onClick(View v) {
		if (v == this.cmdYoutubeOK) {
			this.mY = new YoutubeDownloader(this, this.txtYoutubeUrl.getText().toString());
			this.mY.setProgressDialog(this.pgDialog);
			this.pgDialog.show();
			this.mY.execute("0");
			
		} else if (v == this.cmdInsertDB) {
			MySqlHelper q = new MySqlHelper(this);
			SQLiteDatabase db1 = q.getWritableDatabase();
			db1.beginTransaction();
			Log.d(logName, "inserting 1 record..");
			db1.execSQL("insert into table1(id, name) values(" + String.valueOf(System.currentTimeMillis()) + ", 'Pimsai');");
			Log.d(logName, "inserted 1 record but no commit yet");
			//db1.endTransaction();
			
			db1.setTransactionSuccessful();
			Log.d(logName, "Committed");
		}
		
	}
	
	public void startDownload(String finalUrl, String fileName) {
		this.mY = new YoutubeDownloader(this, this.txtYoutubeUrl.getText().toString());
		this.mY.execute("1", finalUrl, fileName);
		
	}

}
